import open3d as o3d
import open3d.geometry as oge
import os

def test01_for015():
    """
    0.15版本的open3d
    :return:
    """
    """
    open3d.io.read_point_cloud(filename: os.PathLike, format: str = 'auto', 
    remove_nan_points: bool = False, remove_infinite_points: bool = False, 
    print_progress: bool = False) → open3d.geometry.PointCloud
    """
    pcd: oge.PointCloud = o3d.io.read_point_cloud("bunny.pcd")
    print(len(pcd.points))
    o3d.visualization.draw(pcd)
    # write_ascii (bool, optional, default=False)  默认是二进制
    # compressed (bool, optional, default=False)  是否压缩 只有在write_ascii=False 才有用 用gzip 压缩
    """
    open3d.io.write_point_cloud(filename: os.PathLike, pointcloud: open3d.geometry.PointCloud, 
    format: str = 'auto', write_ascii: bool = False, 
    compressed: bool = False, print_progress: bool = False) → bool
    """
    o3d.io.write_point_cloud("copy_bunny1.pcd", pcd,format= 'auto',write_ascii=True)

    pass
def test01_for019():

    # 1. 读取点云
    pcd = o3d.io.read_point_cloud("bunny10k.ply")
    if len(pcd.points) == 0:
        print("错误：bunny.pcd 不存在或为空！")
        exit()
    print(f"成功读取点云，点数: {len(pcd.points)}")
    # 2. 可视化（0.19 正确写法）<br/>
    print("正在打开可视化窗口...")
    o3d.visualization.draw_geometries([pcd])
    # 必须加 [pcd]
    # 3. 保存副本（推荐压缩二进制
    success = o3d.io.write_point_cloud("copy_bunny09.pcd",pcd,write_ascii=False, compressed=True,print_progress=False)
    # 压缩（0.19 支持）
    if success:
        size = os.path.getsize("copy_bunny09.pcd")
        print(f"保存成功！copy_bunny09.pcd 已生成，大小: {size} 字节")
    else:
        print("保存失败！")
    pass

if __name__ == "__main__":
    # test01_for015()
    test01_for019()